package top.tangyh.lamp.care.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.tangyh.basic.context.ContextUtil;
import top.tangyh.basic.exception.BizException;
import top.tangyh.lamp.care.common.SunShangTypeEnum;
import top.tangyh.lamp.care.mapper.BYcSunShangDictMapper;
import top.tangyh.lamp.care.service.BYcSunShangDictService;
import top.tangyh.basic.base.service.impl.SuperServiceImpl;
import top.tangyh.lamp.care.manager.BYcSunShangDictManager;
import top.tangyh.lamp.care.entity.BYcSunShangDict;
import top.tangyh.lamp.care.vo.result.SunShangOutVO;
import top.tangyh.lamp.care.vo.save.BYcSunShangDictSaveVO;
import top.tangyh.lamp.care.vo.update.BYcSunShangDictUpdateVO;
import top.tangyh.lamp.care.vo.result.BYcSunShangDictResultVO;
import top.tangyh.lamp.care.vo.query.BYcSunShangDictPageQuery;
import top.tangyh.lamp.system.entity.tenant.DefUser;
import top.tangyh.lamp.system.service.tenant.DefUserService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 业务实现类
 * 压疮—压力性损伤记录选项字典表
 * </p>
 *
 * @author zqb
 * @date 2024-07-16 14:57:45
 * @create [2024-07-16 14:57:45] [zqb] [代码生成器生成]
 */
@Slf4j
@RequiredArgsConstructor
@Service
@Transactional(readOnly = true)
public class BYcSunShangDictServiceImpl extends SuperServiceImpl<BYcSunShangDictManager, Long, BYcSunShangDict> implements BYcSunShangDictService {

    private final BYcSunShangDictMapper bYcSunShangDictMapper;

    private final DefUserService defUserService;
//    @Resource
//    private  OkHttpClientUtils okHttpClientUtils;
    @Override
    public SunShangOutVO selectSunShangDict() {
        List<BYcSunShangDict> yxYcSunShangDictList = bYcSunShangDictMapper.selectList(null);
        Map<String, List<BYcSunShangDict>> groupList = yxYcSunShangDictList.stream().collect(Collectors.groupingBy(BYcSunShangDict::getType));
        SunShangOutVO sunShangOutVO = new SunShangOutVO();
        //通过type放入对应list
        groupList.forEach((type, dataList) -> SunShangTypeEnum.forValue(type).putList(sunShangOutVO, dataList));
        return sunShangOutVO;
    }

    /**
     * 服务地址 调用his 的同步接口地址
     */
    private String serverAddress = "";
    /**
     * 损伤记录字典uri
     */
    private String sunShangUri = "/topro/inpnurse/third/auditSore/fuse";
    public static final String CODE = "code";

    public static final String MSG = "msg";

    public static final String DATA = "data";

    public static final String SUCCESS = "SUCCESS";

//    @Transactional(rollbackFor = Exception.class)
//    @Override
//    public boolean syncSunShangDictData() {
//        JwtUserDto jwtUserDto = (JwtUserDto) SecurityUtil.getCurrentUser();
//        UserDto user = jwtUserDto.getUser();
//        String empIdent = user.getEmpIdent();
//        ContextUtil.getUserId()
//        DefUser defUser = defUserService.getByIdCache(ContextUtil.getUserId());
//        if (StrUtil.isBlank(defUser.getUsername())) {
//            throw new BizException("账户工号为空，请联系管理员");
//        }
//        String url = serverAddress + sunShangUri + "?hiId=9" + "&uiCode=" + defUser.getUsername()+"&inHospitalStatus=0";
//        log.info("损伤评估字典接口调用url: {}", url);
//        String response = okHttpClientUtils.doGet(url);
//        JSONObject jsonObject = JSONObject.parseObject(response);
//        String code = jsonObject.getString(CODE);
//        if (!StrUtil.equals(SUCCESS, code)) {
//            throw new BizException("同步损伤字典数据失败,请联系管理员");
//        }
//        log.info("outjson: {}", jsonObject.toJSONString());
//        String data = jsonObject.getString(DATA);
        //SunShangOutVO sunShangOutVO = (SunShangOutVO) data;
//        SunShangOutVO sunShangOutVO = JSONObject.parseObject(data, SunShangOutVO.class);
//        List<BYcSunShangDict> list = new ArrayList<>();
//        list.addAll(sunShangOutVO.getBadSmellRadio());
//        list.addAll(sunShangOutVO.getOrientation());
//        list.addAll(sunShangOutVO.getLevel());
//        list.addAll(sunShangOutVO.getStealths());
//        list.addAll(sunShangOutVO.getSinus());
//        list.addAll(sunShangOutVO.getSecretion());
//        list.addAll(sunShangOutVO.getBadSmell());
//        list.addAll(sunShangOutVO.getWoundBase());
//        list.addAll(sunShangOutVO.getStealth());
//        list.addAll(sunShangOutVO.getName());
//        list.addAll(sunShangOutVO.getProgress());
//        list.addAll(sunShangOutVO.getSecretionList());
//        list.addAll(sunShangOutVO.getOoze());
//        list.addAll(sunShangOutVO.getDressingState());
//        list.addAll(sunShangOutVO.getDressingApplication());
//        list.addAll(sunShangOutVO.getPlace());
//        list.addAll(sunShangOutVO.getMeasures());
//        return this.saveBatch(list);
//
//    }
}


